package com.miraksoft.statewatcher.helper;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Chronometer;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.miraksoft.statewatcher.R;
import com.miraksoft.statewatcher.bean.WatchedObjectDef;
import com.miraksoft.statewatcher.bean.WatchedObjectRuntime;
import com.miraksoft.statewatcher.bean.WatchedObjectRuntimeContainer;
import com.miraksoft.statewatcher.bean.WatchedObjectStateDef;
import com.miraksoft.statewatcher.bean.WatchedObjectStateRuntime;

public class WatchedObjectUIHelper {

	/**
	 * create a new UI Button from WatcheObjectState bean
	 * 
	 * @param context
	 * @param watchedObjectState
	 * @return
	 */
	public static Button createStateButton(Context context, WatchedObjectStateDef watchedObjectStateDef){
		Button stateButton = new Button(context);
		
		return stateButton;
	}
	
	public static WatchedObjectRuntimeContainer inflateWatchedObjectsIntoTable( 
			Activity activity,
			TableLayout tableLayout,
			LayoutInflater inflater, 
			List<WatchedObjectStateDef> defStates, 
			List<WatchedObjectDef> defObjects,
			OnClickListener onClickListener)
	{
		
		WatchedObjectRuntimeContainer container = new WatchedObjectRuntimeContainer();
		
		int dynamicId = 0;

		for (WatchedObjectDef defObject : defObjects) {
			
			View watchedObjectRow = inflater.inflate(R.layout.layout_active_session_watched_object, null);
			
			TextView textViewWatchedObjectName = (TextView)watchedObjectRow.findViewById(R.id.textViewWatchedObjectName);
			LinearLayout watchedObjectStatesView = (LinearLayout)watchedObjectRow.findViewById(R.id.linearViewWatchedObjectStates);

			textViewWatchedObjectName.setText(defObject.getName());

			WatchedObjectRuntime runtimeObject = container.addWatchedObjecRuntime(defObject);
			
			for (WatchedObjectStateDef defState : defStates) {
				
				LinearLayout layoutStateRuntime = (LinearLayout)inflater.inflate(R.layout.layout_active_session_watched_object_state, null);
				
				Button btnState = (Button) layoutStateRuntime.findViewById(R.id.btnWatchedObjectStateRuntime);
				btnState.setText(defState.getName());
				btnState.setOnClickListener(onClickListener);
				btnState.setId(++dynamicId);
				
				TextView textViewTimer = (TextView) layoutStateRuntime.findViewById(R.id.textTimerWatchedObjectStateRuntime);
				textViewTimer.setId(++dynamicId);
				
				watchedObjectStatesView.addView(layoutStateRuntime);
				
				WatchedObjectStateRuntime stateRuntime = runtimeObject.addWatchedObjectStateRuntime(defState);
				stateRuntime.setButton(btnState);
				stateRuntime.setTextTimer(textViewTimer);
				
			}
			
			tableLayout.addView(watchedObjectRow);
		}
		
		return container;
	}
	
}
